package com.hspedu.newData.dmsxl.树.二叉树的遍历.层序遍历迭代法.队列;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LC2415反转二叉树的奇数层 {

    public TreeNode reverseOddLevels(TreeNode root) {

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        boolean flag = false;
        while (!queue.isEmpty()){

            int len = queue.size();
            List<TreeNode> arr = new ArrayList<>();
            while (len>0){
                TreeNode node = queue.poll();

                if (flag){
                    arr.add(node);
                }
                if (node.left!=null){
                    queue.offer(node.left);
                    queue.offer(node.right);
                }

                len--;
            }

            if (flag){
                for (int l = 0,r= arr.size()-1; l < r; l++,r--) {
                    int temp = arr.get(l).val;
                    arr.get(l).val = arr.get(r).val;
                    arr.get(r).val = temp;
                }
            }
            flag = !flag;
        }
        return root;
    }

    public int findClosest(int x, int y, int z) {

        int m = Math.abs(z-x);
        int n = Math.abs(z-y);
        if (m>n){
            return 2;
        } else if (m < n) {
            return 1;
        }else {
            return 0;
        }
    }

}
